package com.stk.dots;

import android.graphics.Canvas;
import android.view.SurfaceHolder;

class SurfaceThread extends Thread{
	private SurfaceHolder _surfaceHolder;
	private Panel _panel;
	private boolean _run = false;
	public enum Status {MENU, INSTRUCTIONS, LEVEL_SELECT, PLAYING, PAUSED, WIN, LOOSE, GO_OUT};
	public Status _status = Status.MENU;
	private long startTime;
	private long finishTime;
	private long lastTime;
	private long firstTime = 0;
	
	public SurfaceThread (SurfaceHolder surfaceHolder, Panel panel){
		_surfaceHolder = surfaceHolder;
		_panel = panel;
	}
	
	public void run(){
		Canvas c;
		while (_run){			
			c=null;
			try {
					c = _surfaceHolder.lockCanvas(null);
					synchronized (_surfaceHolder) {	
						lastTime =android.os.SystemClock.elapsedRealtime() - firstTime;
						firstTime = android.os.SystemClock.elapsedRealtime();
						
						switch (_status){
						case PLAYING:{							
							_panel.updateGame(lastTime);
							_panel.drawGame(c);	
						}break;

						case MENU:{
							_panel.drawMenu(c);
						}break;
						
						case INSTRUCTIONS:{
							_panel.drawInstructions(c);
						}break;
						
						case PAUSED:{
							_panel.updatePause(lastTime);
							_panel.drawPaused(c);
						}break;
						
						case LEVEL_SELECT:{
							_panel.drawLevelSelection(c);
						}break;
						
						case WIN:{
							_panel.drawWin(c,finishTime - startTime);
						}break;
						
						case LOOSE:{
							_panel.drawLoose(c,finishTime - startTime);
						}break;
						}
					}
			} finally {
				if (c != null){
					_surfaceHolder.unlockCanvasAndPost(c);
				}
			}			
		}
	}
	
	public void setRunning (boolean run){
		_run = run;
	}
	
	public Status getStatus() {
		return _status;
	}

	public void setStatus(Status status) {
		_status = status;
	}

	public long getLastTime() {
		return lastTime;
	}

	public void setLastTime(long lastTime) {
		this.lastTime = lastTime;
	}

	public long getFirstTime() {
		return firstTime;
	}

	public void setFirstTime(long firstTime) {
		this.firstTime = firstTime;
	}

	public long getStartTime() {
		return startTime;
	}

	public void setStartTime(long startTime) {
		this.startTime = startTime;
	}	
	
	public SurfaceHolder getSurfaceHolder(){
		return _surfaceHolder;
	}
	
	public long getFinishTime() {
		return finishTime;
	}

	public void setFinishTime(long finishTime) {
		this.finishTime = finishTime;
	}


}